package com.bml.www.teacher.teacher;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bml.www.teacher.R;
import com.bml.www.teacher.entity.FileUtils;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

public class SetActivity extends BaseActivity {
    @Bind(R.id.tvCache)
    TextView mTvCache;
    private File cacheDir;
    private String cacheSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initToolBar();
        setCache();

    }

    private void setCache() {
        //计算缓存大小
        cacheDir = getCacheDir();
        cacheSize = FileUtils.size(cacheDir);
        mTvCache.setText(cacheSize+"");
    }

    private void initToolBar() {
        getCustomToolbar().setBackgroundColor(Color.parseColor("#FFFFFF"));
        getCustomToolbar().setTitle("设置");
        getCustomToolbar().addLeftImageButton(getResources().getDrawable(R.mipmap.backby), new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_set, null);
    }


    @OnClick({R.id.rl_modify, R.id.rl_clear, R.id.rl_feedback, R.id.rl_our})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_modify:

                break;
            case R.id.rl_clear:
                //弹出Dialog
                showCacheDialog(cacheDir, cacheSize);
                break;
            case R.id.rl_feedback:
                break;
            case R.id.rl_our:
                break;
        }
    }

    private void showCacheDialog(File cacheDir, String cacheSize) {
        if ("0B".equals(cacheSize)) {
            final AlertDialogFragment nullCacheDialogFrag = new AlertDialogFragment();
            nullCacheDialogFrag.setMessage("无缓存")
                    .setPositiveButton("确认", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            nullCacheDialogFrag.dismiss();
                        }
                    }).show(getSupportFragmentManager());
        } else {
            final AlertDialogFragment cleanDialogFrag = new AlertDialogFragment();
            cleanDialogFrag.setMessage("确认清除" + cacheSize + "缓存?")
                    .setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            cleanDialogFrag.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            FileUtils.delFilesFromPath(cacheDir);
                            cleanDialogFrag.dismiss();
                            mTvCache.setText(11+"");
                            Toast.makeText(SetActivity.this, "已清除" + cacheSize + "缓存!", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .show(getSupportFragmentManager());
        }


    }
}
